\documentclass[a4paper,12pt]{article}

\usepackage[margin=1.5cm]{geometry}
\usepackage{listings}
\usepackage{enumitem}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,shapes,calc}

\title{Tikz Examples}
\author{Chen Rushan\\chenrsster@gmail.com}
\date{2009.12.03 13:43}

\setlength{\parskip}{1.2ex}

\lstset{
    frame=single, framesep=0pt, framexleftmargin=-1pt, framextopmargin=3pt,
    framexbottommargin=3pt, aboveskip=0.5cm, belowskip=0.2cm, columns=flexible,
    basicstyle=\ttfamily
}

\begin{document}

\maketitle

\begin{figure}[ht]
    \begin{tikzpicture}[line/.style={draw, thick, -latex', shorten >= 2cm, shorten <= 2cm}, every node/.style={scale=.8}, scale=.8]
        \node (origin) {\lstinline=NP=} [sibling distance=10mm]
            child {node {\lstinline=NN=}}
            child {node {\lstinline=CC=}}
            child {node {\lstinline=NN=}}
            child {node {\lstinline=NN=}};

        \node (vertical) [right=of origin, xshift=4cm] {\lstinline=NP^g=} [sibling distance=10mm]
            child {node {\lstinline=NN=}}
            child {node {\lstinline=CC=}}
            child {node {\lstinline=NN=}}
            child {node {\lstinline=NN=}};

        \coordinate (a) at ($ (origin) + (0, -0.7cm) $);
        \coordinate (b) at ($ (vertical) + (0, -0.7cm) $);
        \path[line] (a) -- (b);

        \node (binarize) [right=of vertical, xshift=4cm] {\lstinline=NP^g=} 
            child {[sibling distance=20mm] node {\lstinline=@NP^g->_NN=}
                child {[sibling distance=10mm]node {\lstinline=@NP^g->_NN_NN=}
                    child {node {\lstinline=NN=}}
                    child {node{\lstinline=CC=}}}
                child {node {\lstinline=NN=}}}
            child {node {\lstinline=NN=}};

        \coordinate (a) at ($ (vertical) + (0, -.7cm) $);
        \coordinate (b) at ($ (binarize) + (0, -.7cm) $);
        \path[line] (a) -- (b);

        \node (horizontal) [right=of binarize, xshift=4cm] {\lstinline=NP^g=}
            child {[sibling distance=20mm] node {\lstinline=@NP^g=}
                child {[sibling distance=10mm]node {\lstinline=@NP^g=}
                    child {node {\lstinline=NN=}}
                    child {node{\lstinline=CC=}}}
                child {node {\lstinline=NN=}}}
            child {node {\lstinline=NN=}};
        \coordinate (a) at ($ (binarize) + (0, -.7cm) $);
        \coordinate (b) at ($ (horizontal) + (0, -.7cm) $);
        \path[line] (a) -- (b);
    \end{tikzpicture}
    \caption{}
\end{figure}

\begin{figure}[ht]
\begin{minipage}{0.5\textwidth}
    \centering
    \begin{tikzpicture}
        [every node/.style={scale=.8},
         every path/.style={thick, draw},
         scale=.8]
        \node (A) {A};
        \node (B) [below=of A, xshift=-1.2cm] {B};
        \node (C) [below=of A, xshift=1.2cm] {C};

        \path[-latex'] (A.south) .. controls +(-0.8, -0.5) .. (B.north);
        \path[-latex'] (A.south) .. controls +(0.8, -0.5) .. (C.north);
        \path (B.south) -- +(-1, -0.5) coordinate[label=below:i] -- +(1, -0.5) coordinate[label=below:k] -- cycle;
        \path (C.south) -- +(-0.9, -0.5) coordinate[label=below:k+1] -- +(1, -0.5) coordinate[label=below:j] -- cycle;
    \end{tikzpicture}
    \caption{}
    
    \vspace{1cm}
    
    \begin{tikzpicture}
        [every node/.style={minimum height=1.5cm, text width=3.5cm, scale=0.8, thick, draw, text centered, rectangle},
         every path/.style={-latex', thick, draw},
         scale=0.8]

        \node (first) {create multiple levels of grammars from coarse to fine};
        \node (second) [below=of first] {coarse-to-fine preparse};
        \node (third) [below=of second] {MaxRule finalparse};

        \path (first) -- (second);
        \path (second) -- (third);
    \end{tikzpicture}
    \caption{}
\end{minipage}
\begin{minipage}{0.5\textwidth}
    \centering
    \begin{tikzpicture}
        [start/.style={draw=blue, thick, fill=blue!20, ellipse, inner sep=1pt, text width=2cm, text centered, minimum height=1.2cm, scale=.8},
         function/.style={draw=blue, thick, fill=blue!20, rectangle, text centered, text width=3.5cm, inner sep=3pt, scale=.8},
         decide/.style={draw=blue, thick, fill=blue!20, diamond, text badly centered, inner sep=3pt, text width=2.5cm, scale=.8},
         line/.style={draw, thick, -latex'},
         scale=.8]

        \matrix[row sep=7mm]
        {
            \node(start)  [start]    {start}; \\
            \node(fun1)   [function] {fetch grammar of the next level}; \\
            \node(decide) [decide]   {grammar reaches desired level}; \\
            \node(fun2)   [function] {calculate the viterbi version inside and outside score}; \\
            \node(fun3)   [function] {prune chart according to inside and outside score}; \\
            \node(end)    [start]    {end}; \\
        };

        \begin{scope}[every path/.style=line]
            \path (start) -- (fun1);
            \path (fun1) -- (decide);
            \path (decide) -- node {no} (fun2);
            \path (decide) .. controls +(right:4cm) and +(right:4cm) .. node {yes} (end);
            \path (fun2) -- (fun3);
            \path (fun3) .. controls +(left:4cm) and +(left:4cm) .. (fun1);
        \end{scope}
    \end{tikzpicture}
    \caption{}
\end{minipage}
\end{figure}

\begin{figure}[ht]
    \centering
    \begin{tikzpicture}
        [every path/.style={draw, -latex', thick},
         every node/.style={rectangle, text centered, draw, scale=.8},
         scale=.8]

        % \newline should be used in combination with (text width) option, or it will have no effect.

        \node(getBestConstrainedParse 1) {getBestConstrainedParse(1)};
        \node(getBestConstrainedParse 2) [below=of getBestConstrainedParse 1, yshift=-5cm] {getBestConstrainedParse(2)};
        \node(createArrays) [right=of getBestConstrainedParse 1, xshift=9cm, yshift=3cm] {createArrays};
        \node(initializeChart) [below=of createArrays] {initializeChart};
        \node(insideScore) [below=of initializeChart, text width=6cm, text justified]
                            {if grammar level $<$ 1 \newline doConstrainedViterbiInsideScores \newline else \newline doConstrainedInsideScores};
        \node(outsideScore) [below=of insideScore, text width=6.5cm, text justified]
                            {if grammar level $<$ 1 \newline doConstrainedViterbiOutsideScores \newline else \newline doConstrainedOutsideScores};
        \node(pruneChart) [below=of outsideScore] {pruneChart};
        \node(doPreParse) [right=of getBestConstrainedParse 1, xshift=2cm, yshift=-1.5cm] {doPreParse};
        \node(createArrays 2) [below=of doPreParse, yshift=-6cm] {createArrays};
        \node(initializeChart 2) [below=of createArrays 2] {initializeChart};
        \node(doConstrainedInsideScores) [below=of initializeChart 2] {doConstrainedInsideScores};
        \node(doConstrainedOutsideScores) [below=of doConstrainedInsideScores] {doConstrainedOutsideScores};
        \node(doConstrainedMaxCScores) [below=of doConstrainedOutsideScores] {doConstrainedMaxCScores};
        \node(extractBestMaxRuleParse) [below=of doConstrainedMaxCScores] {extractBestMaxRuleParse};
        \node(extractBestMaxRuleParse1) [right=of extractBestMaxRuleParse, xshift=1cm, yshift=1cm] {extractBestMaxRuleParse1};
        \node(extractBestMaxRuleParse2) [below=of extractBestMaxRuleParse1] {extractBestMaxRuleParse2};

        \path (getBestConstrainedParse 1) +(up:3cm) -- (getBestConstrainedParse 1);
        \path (getBestConstrainedParse 1) -- (getBestConstrainedParse 2);
        \path (getBestConstrainedParse 2.north) .. controls +(up:3cm) and +(left:3cm) .. (doPreParse.west);
        \path (getBestConstrainedParse 2.south) .. controls +(down:2cm) and +(left:4cm) .. (createArrays 2);
        \path (getBestConstrainedParse 2.south) .. controls +(down:3.5cm) and +(left:4cm) .. (initializeChart 2);
        \path (getBestConstrainedParse 2.south) .. controls +(down:5cm) and +(left:4cm) .. (doConstrainedInsideScores);
        \path (getBestConstrainedParse 2.south) .. controls +(down:6.5cm) and +(left:4cm) .. (doConstrainedOutsideScores);
        \path (getBestConstrainedParse 2.south) .. controls +(down:8cm) and +(left:4cm) .. (doConstrainedMaxCScores);
        \path (getBestConstrainedParse 2.south) .. controls +(down:9.5cm) and +(left:4cm) .. (extractBestMaxRuleParse);
        \path (doPreParse.east) .. controls +(up:4cm) and +(left:2cm) .. (createArrays.west);
        \path (doPreParse.east) .. controls +(up:2cm) and +(left:2cm) .. (initializeChart.west);
        \path (doPreParse.east) to (insideScore.west);
        \path (doPreParse.east) .. controls +(down:1cm) and +(left:2cm) .. (outsideScore.west);
        \path (doPreParse.east) .. controls +(down:3cm) and +(left:3cm) .. (pruneChart.west);
        \path (extractBestMaxRuleParse.east) .. controls +(up:1cm) and +(left:1cm) .. (extractBestMaxRuleParse1.west);
        \path (extractBestMaxRuleParse.east) .. controls +(down:1cm) and +(left:1cm) .. (extractBestMaxRuleParse2.west);
        \path (extractBestMaxRuleParse1) edge (extractBestMaxRuleParse2);
        \path (extractBestMaxRuleParse2) edge (extractBestMaxRuleParse1);
    \end{tikzpicture}
    \caption{}
\end{figure}

\end{document}

